﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;

namespace giaodien
{
    public partial class txttongtien : Form
    {
        public txttongtien()
        {
            InitializeComponent();
        }

        static string Ten;
        static string Ma;
      //  string mahoadon;
        DataTable dt = new DataTable();
        DataTable tkhd = new DataTable();
        DataTable cthd = new DataTable();
        DataTable dttknam = new DataTable();
        ConnectData obj = new ConnectData();
        DataTable dshd = new DataTable();
        Boolean flag = false;
        int chon = 0;
        int tien;
        #region xuly cac nut
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            UI.MouseDown(e);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            UI.MouseMove(this, MousePosition.X, MousePosition.Y);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            UI.MouseUp();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            UI.Exit(this);
        }
        #endregion
        #region xuly listview tro sang listview
        //lay ten nguoi dang nhap
        public static void LayTen(string TenGui, string MaGui)
        {
            Ten = TenGui;
            Ma = MaGui;
        }
        private void ThongKeBanHang_Load(object sender, EventArgs e)
        {
            txtnguoithongke.Text = Ten;
            tkhd = obj.laydulieuBang("select * from HoaDon");
            hienthihoadon(tkhd);
            lbltongtien.Text = obj.tinhtong() + " VNĐ";
            //chạy lấy hóa đơn theo tháng năm
            dttknam = obj.laydulieuBang("SELECT distinct HoaDon.NgayLapHD FROM HoaDon;");
            object[] Nam = new object[0];
            object[] Tam;
            int j = 0;
            for (int i = 0; i < dttknam.Rows.Count; i++)
            {
                if (Nam.Contains(DateTime.Parse(dttknam.Rows[i][0].ToString()).Year) == false)
                {

                    Tam = Nam;
                    Nam = ChuyenMang(Nam, Tam);
                    Nam[j] = DateTime.Parse(dttknam.Rows[i][0].ToString()).Year;
                    j++;
                }
            }
            cmbNam.Items.AddRange(Nam);
            chkNam.Checked = true;
            cmbNam.SelectedIndex = 0;
            cmbThang.SelectedIndex = 0;
        }
        //hiển thị hóa đơn
        void hienthihoadon(DataTable dt)
        {
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem it = new ListViewItem();
                it.Text = dr["MaHD"].ToString();
                it.SubItems.Add(string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(dr["NgayLapHD"].ToString()).ToShortDateString()));
                it.SubItems.Add(dr["TenKhachHang"].ToString());
                it.SubItems.Add(dr["MaNV"].ToString());
                it.SubItems.Add(dr["TongTien"].ToString());
                listView1.Items.Add(it);
               
            }
        }
        // hiển thị chi tiết hóa đơn
        void chitiethoadon(DataTable dt)
        {
            listView2.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem id = new ListViewItem();
                id.Text = dr["MaHD"].ToString();
                id.SubItems.Add(dr["MaSP"].ToString());
                id.SubItems.Add(dr["SoLuongBan"].ToString());
                id.SubItems.Add(dr["ThanhTien"].ToString());
                listView2.Items.Add(id);
            }
        }
        //tạo sự kiện đổ dữ liệu xuống chi tiết hóa đơn
        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            ListViewItem it = new ListViewItem();
            it = listView1.GetItemAt(e.X, e.Y);
            DataTable dtcthd = new DataTable();
            if(it!=null)
            dtcthd = obj.laydulieuBang("Select * from CTHD where MaHD='" + it.SubItems[0].Text + "'");
            chitiethoadon(dtcthd);

        }
        #endregion

        #region thongketheothangnam
        #region lenh lay dl theo thang nam
        private void xemtheothang(int thang)
        {
            string sql = "select * from HoaDon where NgayLapHD like '%/" + thang + "/%'";
            dt = obj.laydulieuBang(sql);
            hienthihoadon(dt);
        }
        private void xemtheonam(int nam)
        {
            string sql = "select * from HoaDon where NgayLapHD  like  '%/" + nam + "'";
            dt = obj.laydulieuBang(sql);
            hienthihoadon(dt);
        }
        private void xemtheonamthang(int thang, int nam)
        {
            string sql = "select * from HoaDon where NgayLapHD like '%" + thang + "/" + nam + "'";
            dt = obj.laydulieuBang(sql);
            hienthihoadon(dt);
        }
        #endregion
        void capnhatlaiitem(DataRow dr)
        {
            ListViewItem it = new ListViewItem();
            it.Text = dr[0].ToString();
            it.SubItems.Add(dr[1].ToString());
            it.SubItems.Add(dr[2].ToString());
            it.SubItems.Add(dr[3].ToString());
            it.SubItems.Add(dr[4].ToString());
            listView1.Items.Add(it);
        }
        private object[] ChuyenMang(object[] Nam, object[] Tam)
        {
            Nam = new object[Tam.Length + 1];
            for (int i = 0; i < Tam.Length; i++)
            {
                Nam[i] = Tam[i];
            }
            return Nam;
        }
        private void loadlistviewtheoloai()
        {
            if (chkNam.Checked == true && chkThang.Checked == true)
            {
                chon = 1;
            }
            else
            {
                if (chkNam.Checked == true && chkThang.Checked == false)
                {
                    chon = 2;
                }
                else
                {
                    if (chkNam.Checked == false && chkThang.Checked == true)
                    {
                        chon = 3;
                    }
					else{
						chon=0;
					}
                }
            }
        }
        void loadtheodk(int thang, int nam)
        {
            switch (chon)
            {
                case 1:
                    {
                        xemtheonamthang(thang, nam);
                        break;
                    }
                case 2:
                    {
                        xemtheonam(nam);
                        break;
                    }
                case 3:
                    {
                        xemtheothang(thang);
                        break;
                    }
            }
        }
        private void chkThang_CheckedChanged(object sender, EventArgs e)
        {
            {
                if (cmbNam.SelectedItem == null || cmbThang.SelectedItem == null)
                    return;

                loadlistviewtheoloai();
                loadtheodk(int.Parse(cmbThang.SelectedItem.ToString()), int.Parse(cmbNam.SelectedItem.ToString()));
            }
        }
        private void cmbThang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNam.SelectedItem == null || cmbThang.SelectedItem == null)
                return;

            loadlistviewtheoloai();
            loadtheodk(int.Parse(cmbThang.SelectedItem.ToString()), int.Parse(cmbNam.SelectedItem.ToString()));
        }
        #endregion   
    }
}
